<?php

namespace app\admin\model\user;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Model;


class Usertwo extends Model
{


    // 表名
    protected $name = 'user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_experience_store_text',
        'prevtime_text',
        'logintime_text',
        'jointime_text',
        'status_text',
        'team_status',
        'team_withdraw',
        'team_firepower'
    ];

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password'])) {
                if ($changed['password']) {
                    $salt          = \fast\Random::alnum();
                    $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'],
                        $salt);
                    $row->salt     = $salt;
                } else {
                    unset($row->password);
                }
            }
        });
    }

    public function getIsExperienceStoreList()
    {
        return ['1' => __('Is_experience_store 1'), '0' => __('Is_experience_store 0')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('禁用')];
    }

    public function getPayList()
    {
        return ['money' => '余额', 'score' => '积分','score_gift'=>'已赠送', 'golden' => '金豆', 'gold' => '金币', 'silver' => '银豆','firepower'=>'贡献值'];
    }

    public function paRateList()
    {
        return [0 => '0', 10 => '10%', 15 => "15%"];
    }

    public function getTeamStatusAttr($value,$data)
    {
        if(isset($data['id'])){
            $childrenModel = new \app\admin\model\Children;
            $children = $childrenModel->where(['member_id' => $data['id']])->column('children_id');
            if (!$children) {
                $value = 0;
            } else {
                $count = self::whereIn('id', $children)->where('status', 'normal')->count();
                if ($count) {
                    $value = 1;
                } else {
                    $value = 2;
                }
            }
        }
       return $value;
    }
    public function getTeamWithdrawAttr($value,$data)
    {
        if(isset($data['id'])){
            $childrenModel = new \app\admin\model\Children;
            $children = $childrenModel->where(['member_id' => $data['id']])->column('children_id');
            if (!$children) {
                $value = 0;
            } else {
                $count = self::whereIn('id', $children)->where('is_withdraw', 1)->count();
                if ($count) {
                    $value = 1;
                } else {
                    $value = 2;
                }
            }
        }
        return $value;
    }

    public function getTeamFirepowerAttr($value,$data)
    {
        if(isset($data['id'])){
            $childrenModel = new \app\admin\model\Children;
            $children = $childrenModel->where(['member_id' => $data['id']])->column('children_id');
            if (!$children) {
                $value = 0;
            } else {
                $count = self::whereIn('id', $children)->where('is_firepower', 1)->count();
                if ($count) {
                    $value = 1;
                } else {
                    $value = 2;
                }
            }
        }
        return $value;
    }

    public function getIsExperienceStoreTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_experience_store']) ? $data['is_experience_store'] : '');
        $list  = $this->getIsExperienceStoreList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPrevtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setJointimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function parentuser()
    {
        return $this->belongsTo('app\admin\model\user\Usertwo', 'parent_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
